package library;

import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.faces.util.logging.Logger;
import com.liferay.faces.util.logging.LoggerFactory;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.theme.ThemeDisplay;
import com.slayer.library.model.LMS;
import com.slayer.library.service.LMSLocalServiceUtil;

public class LibraryManagedBean{
	LibraryBackingBean libraryBackingBean = new LibraryBackingBean(); 
	LibraryBackingBean searchBackingBean = new LibraryBackingBean();
	ThemeDisplay themeDisplay = (ThemeDisplay)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(WebKeys.THEME_DISPLAY);
	private static Logger logger = LoggerFactory.getLogger(LibraryManagedBean.class);
	
	private LibraryBackingBean editBookBean;
	private List<LMS>searchBookList;
	
	
	
	//private LibraryBackingBean bookDetails;
	

	public List<LMS> getSearchBookList() {
		return searchBookList;
	}

	public void setSearchBookList(List<LMS> searchBookList) {
		this.searchBookList = searchBookList;
	}

	public LibraryBackingBean getLibraryBackingBean() {
		return libraryBackingBean;
	}

	public void setLibraryBackingBean(LibraryBackingBean libraryBackingBean) {
		this.libraryBackingBean = libraryBackingBean;
	}
	public LibraryBackingBean getSearchBackingBean() {
		return searchBackingBean;
	}

	public void setSearchBackingBean(LibraryBackingBean searchBackingBean) {
		this.searchBackingBean = searchBackingBean;
	}
	public void setBookDetails(LibraryBackingBean bookDetails) {
		System.out.println("Set Book Details -----setter");
		this.libraryBackingBean = bookDetails;
		//bookDetails.persistBookDetails(this.bookDetails);
	}
	
	public LibraryBackingBean getEditBookBean() {
		return editBookBean;
	}

	public void setEditBookBean(LibraryBackingBean editBookBean) {
		this.editBookBean = editBookBean;
		System.out.println("Book Title Setter-------"+this.editBookBean.getBookTitle());
		this.editBookBean.setValues(editBookBean);
	}
	
	public void addBook(){
		System.out.println("Add Book Request Initiated");
		String bookTitle =this.libraryBackingBean.getBookTitle();
		String author = this.libraryBackingBean.getAuthor();
		System.out.println("Book Title : "+bookTitle+" Author : "+author);
		insertBookDetails(bookTitle,author);
		refresh();
	}

	private void insertBookDetails(String bookTitle, String author) {
		LMS lms = null;
		try {
			lms = LMSLocalServiceUtil.createLMS(CounterLocalServiceUtil.increment(LMS.class.getName()));
			lms.setBookTitle(bookTitle);
			lms.setAuthor(author);
			lms.setUserName(themeDisplay.getUser().getScreenName());
			lms.setCreatorId(themeDisplay.getUserId());
			lms.setCreateDate(new Date());
			LMSLocalServiceUtil.addLMS(lms);
			logger.info("Book added successfully");
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "A new book added into Library", ""));
			
		} catch (SystemException e) {
			logger.error(e);
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Your request failed to procede", ""));
			/*
			for(LMS tempLms : searchBookList){
				tempLms.g
			}*/
		}
		
	}
	public void searchBook(){
		System.out.println("----------------------------Inside method - searchBook=====================>>>>>>>>>>>>>>");
		System.out.println("Book Title : "+this.libraryBackingBean.getBookTitle());
		System.out.println("Author : "+this.libraryBackingBean.getAuthor());
		
		
		
	}
	public void refresh() {
	    FacesContext context = FacesContext.getCurrentInstance();
	    Application application = context.getApplication();
	    ViewHandler viewHandler = application.getViewHandler();
	    UIViewRoot viewRoot = viewHandler.createView(context, context
	     .getViewRoot().getViewId());
	    context.setViewRoot(viewRoot);
	    
	   // context.renderResponse(); //Optional
	  }
	@PostConstruct
	public void init(){
		System.out.println("Init methos called");
		try {
			searchBookList = LMSLocalServiceUtil.getLMSs(0, -1);
		} catch (SystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void editBookImpl(){
		System.out.println("-------------------Edit Book Details Impl-------------------");
	}
}
